<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * flexigrid插件类
 * @package  htdocs
 * create by allen.qiang
 * @version 1.0
 */


class sysFlexigridLog {

    /**
     * 查询flexigrid信息
     * @param array $arg_array 参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return array|null
     */
    public function searchFlexigridInfo( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //表ID
        if (isset($arg_array['table_id']) && is_numeric($arg_array['table_id'])) {
            $sql .= ' AND table_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['table_id'];
        }

        //会员号
        if (isset($arg_array['userID']) && !empty($arg_array['userID'])) {
            $sql .= ' AND userID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['userID'];
        }

        //列初始化位置
        if (isset($arg_array['cid']) && is_numeric($arg_array['cid']) ) {
            $sql .= ' AND cid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cid'];
        }

        //最近列位置
        if (isset($arg_array['last_cid']) && is_numeric($arg_array['last_cid']) ) {
            $sql .= ' AND last_cid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['last_cid'];
        }

        //列是否隐藏
        if (isset($arg_array['hide']) && is_numeric($arg_array['hide']) ) {
            $sql .= ' AND hide = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['hide'];
        }

        //列的宽度
        if (isset($arg_array['width']) && is_numeric($arg_array['width']) ) {
            $sql .= ' AND width = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['width'];
        }

        //对齐方式
        if (isset($arg_array['align']) && !empty($arg_array['align']) ) {
            $sql .= ' AND align = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['align'];
        }

        //绑定的列名
        if (isset($arg_array['name']) && !empty($arg_array['name']) ) {
            $sql .= ' AND name = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['name'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_flexigrid_log ' . $sql;
                $count_array = $G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_flexigrid_log ' . $sql;

            $temp_item_array = $G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array) {
                return null;
            }

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'createTime' => $item->createTime,
                    'table_id' => $item->table_id,
                    'userID' => $item->userID,
                    'cid' => $item->cid,
                    'last_cid' => $item->last_cid,
                    'hide' => $item->hide,
                    'width' => $item->width,
                    'align' => $item->align,
                    'name' => $item->name,
                    'sortable' => $item->sortable,
                    'display' => $item->display
                );

            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 新增flexigrid表
     */
    public function editFlexigrid($arg_array){

        global $G_SHOP;

        if(!isset($arg_array['table_id']) || !is_numeric($arg_array['table_id']) || empty($arg_array['table_id'])){
            return array( 'code' => 400, 'message' => '表table_id参数错误' );
        }

        if(!isset($arg_array['userID']) || empty($arg_array['userID'])){
            return array( 'code' => 400, 'message' => '缺少后台会员名' );
        }

        if(!isset($arg_array['data']) || !is_array($arg_array['data']) || empty($arg_array['data'])){
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try{

            foreach($arg_array['data'] as $key=>$flex){

                $sys_flex_log = new entity('sys_flexigrid_log');
                $sys_flex_log->table_id = $arg_array['table_id'];
                $sys_flex_log->userID = $arg_array['userID'];
                $sys_flex_log->creator = 'system';
                $sys_flex_log->cid = $key;

                if( $G_SHOP->DBCA->fetch($sys_flex_log) ){
                    continue;
                }

                $sys_flex_log->last_cid = $key;

                $sys_flex_log->display = $flex['display'];

                if(isset($flex['hide']) && is_numeric($flex['hide'])){
                    $sys_flex_log->hide = $flex['hide'];
                }

                if(isset($flex['width']) && is_numeric($flex['width'])){
                    $sys_flex_log->width = $flex['width'];
                }

                if(isset($flex['align']) && !empty($flex['align'])){
                    $sys_flex_log->align = $flex['align'];
                }

                if(isset($flex['name']) && !empty($flex['name'])){
                    $sys_flex_log->name = $flex['name'];
                }

                if(isset($flex['sortable']) && !empty($flex['sortable'])){
                    $sys_flex_log->sortable = $flex['sortable'];
                }

                $G_SHOP->DBCA->save($sys_flex_log);

            }

            return array( 'code'=>200, 'message'=>'保存成功', 'data'=> $arg_array) ;

        }catch (Exception $e){

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


}
